/*
 * Pastebin.cs
 * 
 * Copyright © 2008 David Paleino <d.paleino@gmail.com>
 * 
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *   
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *   
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, see
 *   <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections;

namespace HanskaLabs.Pastebin
{
	/// <summary>
	/// Pastebin framework
	/// </summary>
	public class Pastebin
	{
		/// <summary>
		/// List of available pastebins (i.e. implemented)
		/// </summary>
		/// <remarks>
		/// This list must contain <b>instances</b> of the implemented classes
		/// (because we use Reflection on them).
		/// </remarks>
		private IPastebin[] available = {
			new DebianNet(),
		};
		
		/// <summary>
		/// Default constructor
		/// </summary>
		public Pastebin()
		{
		}
		
		/// <summary>
		/// Get a pastebin instance
		/// </summary>
		/// <param name="name">
		/// A <see cref="System.String"/>, the Pastebin name
		/// </param>
		/// <returns>
		/// A <see cref="IPastebin"/> instance
		/// </returns>
		public IPastebin GetPastebin(string name) {
			foreach (IPastebin p in this.available) {
				Details d = GetDetails(p);
				if (d.Name.Equals(name)) {
					return p;
				}
			}
			return null;
		}
		
		/// <summary>
		/// Lists available pastebins
		/// </summary>
		/// <returns>
		/// An array of <see cref="System.String"/>s.
		/// </returns>
		public string[] GetAvailablePastebins() {
			ArrayList ret = new ArrayList();
			foreach (IPastebin p in this.available) {
				Details d = GetDetails(p);
				ret.Add(d.Name);
			}
			return (string[])ret.ToArray(typeof(string));
		}
		
		/// <summary>
		/// Gets available languages
		/// </summary>
		/// <param name="pastebin">
		/// A <see cref="System.String"/>, the name of the pastebin
		/// </param>
		/// <returns>
		/// An array of <see cref="System.String"/>, the supported languages
		/// </returns>
		public string[] GetAvailableLanguages(string pastebin) {
			foreach (IPastebin p in this.available) {
				Details d = GetDetails(p);
				if (d.Name.Equals(pastebin)) {
					return p.GetLanguages();
				}
			}
			return null;
		}
		
		/// <summary>
		/// Gets available expire times
		/// </summary>
		/// <param name="pastebin">
		/// A <see cref="System.String"/>, the name of the pastebin
		/// </param>
		/// <returns>
		/// An array of <see cref="System.Int32"/>, the supported expire times
		/// (number of seconds after the paste submit)
		/// </returns>
		public int[] GetAvailableExpires(string pastebin) {
			foreach (IPastebin p in this.available) {
				Details d = GetDetails(p);
				if (d.Name.Equals(pastebin)) {
					return p.GetExpires();
				}
			}
			return null;
		}
		
		/// <summary>
		/// Gets the default language
		/// </summary>
		/// <param name="pastebin">
		/// A <see cref="System.String"/>, the name of the pastebin
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>, the default language
		/// </returns>
		public string GetDefaultLanguage(string pastebin) {
			foreach (IPastebin p in this.available) {
				Details d = GetDetails(p);
				if (d.Name.Equals(pastebin)) {
					return p.DefaultLanguage;
				}
			}
			return "";
		}
		
		/// <summary>
		/// Gets the default expiration time
		/// </summary>
		/// <param name="pastebin">
		/// A <see cref="System.String"/>, the name of the pastebin
		/// </param>
		/// <returns>
		/// A <see cref="System.Int32"/>, the default expiration time (number of
		/// seconds after paste submit)
		/// </returns>
		public int GetDefaultExpire(string pastebin) {
			foreach (IPastebin p in this.available) {
				Details d = GetDetails(p);
				if (d.Name.Equals(pastebin)) {
					return p.DefaultExpiration;
				}
			}
			return 0;
		}
		
		private Details GetDetails(IPastebin pastebin) {
			Attribute[] attrs = Attribute.GetCustomAttributes(pastebin.GetType());
			
			foreach (Attribute attr in attrs) {
				if (attr is Details) {
					return (Details)attr;
				}
			}
			return null;
		}
	}
}
